
enum KEYLIST {
    //% block="A"
    0,
    //% block="B"
    2
    //% block="C"
    1,
    //% block="D"
    3
    //% block="E(↑)"
    4,
    //% block="F(←)"
    6    
    //% block="G(↓)"
    5,
    //% block="H(→)"
    7
}

enum TRACK_CHLIST {
    //% block="T0"
    0,
    //% block="T1"
    2
    //% block="T2"
    1,
    //% block="T3"
    3
    //% block="T4"
    4,
    //% block="T5"
    6    
    //% block="T6"
    5,
    //% block="T7"
    7
}

enum BEATLIST {
    //% block="1/8"
    125, 
    //% block="1/4"
    250,
    //% block="1/2"
    500, 
    //% block="1"
    1000,
    //% block="2"
    2000
}

enum MUSICLIST {
    //% block="生日快乐"
    birthday, 
    //% block="欢乐颂"
    happy,
    //% block="葫芦娃"
    gourd,
    //% block="两只老虎"
    g_tiggers,
    //% block="女海盗"
    Pirates,
    //% block="疯狂青蛙"
    CrazyFrog,
    //% block="马里奥"
    MarioUW,
    //% block="泰坦尼克"
    Titanic
}
enum MCHLIST {
    //% block="L"
    0,
    //%block="R"
    1
}
enum ONOFFLIST {
    //% block="开"
    LOW, 
    //% block="关"
    HIGH
}

enum FPV_DATACH {
    //% block="右摇杆X轴"
    Joystick_RX,
    //% block="右摇杆Y轴"
    Joystick_RY,
    //% block="左摇杆X轴"
    Joystick_LX,
    //% block="左摇杆Y轴"
    Joystick_LY,
    //% block="一键起飞"
    Takeoff,
    //% block="一键降落"
    Land,
    //% block="紧急停止"
    Stop,
    //% block="无头模式"
    Nodir,
    //% block="陀螺仪校正"
    Rectify
}

//% color="#5ba55b" iconWidth=50 iconHeight=40
namespace SmartCar { 
    //% block="读取2.4G遥控手柄#[KEY]" blockType="boolean"
    //% KEY.shadow="dropdown" KEY.options="KEYLIST"
    export function SmartCar_emitter(parameter: any, block: any) {
        let key = parameter.KEY.code;
        Generator.addInclude('include_RL_PCF8574T', '#include "RL_PCF8574T.h"');
        Generator.addObject(`object_PCF8574_emitter`, `PCF8574 `,  `emitter(0x20);`);
        Generator.addSetup(`setup_emitter_begin`, `emitter.begin();`);
        Generator.addSetup(`setup_pcf8574_pinMode${key}`, `emitter.pinMode(${key}, INPUT);`);
        Generator.addCode(`emitter.digitalRead(${key})`);
    }
    //% block="读取电池电压" blockType="reporter"
    export function SmartCar_vol(parameter: any, block: any) {
        Generator.addInclude('include_RL_SmartCar', '#include "RL_SmartCar.h"');
        Generator.addObject(`object_SmartCar_car`, `SmartCar`,  ` car;`);
        Generator.addCode(`car.car_vol()`);
    }
    //% block="读取超声波传感器#[ULRASONIC]" blockType="reporter"
    //% ULRASONIC.shadow="dropdown"  ULRASONIC.options="SmartCar_ULRASONIC_PIN"
    export function SmartCar_Ulrasonic(parameter: any, block: any) {  
        let pin = parameter.ULRASONIC.code;    
        Generator.addInclude("include_Ultrasonic", `#include "RL_Ultrasonic.h"`);
        Generator.addObject("object_Ultrasonic", `Ultrasonic `, `ultrasonic(${pin});`);
        Generator.addCode(`ultrasonic.MeasureInCentimeters()`);
    }
    //% block="读取寻迹传感器#[TRACK]" blockType="boolean"
    //% TRACK.shadow="dropdown" TRACK.options="SmartCar_TRACK_PIN"
    export function SmartCar_track(parameter: any, block: any) {
        let pin = parameter.TRACK.code;
       // Generator.addSetup(`pinMode`, `pinMode(${pin}, INPUT);`);
        Generator.addCode(`analogRead(${pin})>100?1:0`);
    }
    //% block="读取双路寻迹传感器位置（1-5）" blockType="reporter"
    export function SmartCar_trackrlmiddle(parameter: any, block: any) {
        Generator.addInclude("include_RL_SmartCar", `#include"RL_SmartCar.h"`);
        Generator.addObject("object_SmartCar_trackrl",`SmartCar`,  ` trackrl;`);
        Generator.addCode(`trackrl.findline()`);
    }
    //% block="读取寻迹传感器#[TRACK_CH]" blockType="boolean"
    //% TRACK_CH.shadow="dropdown" TRACK_CH.options="TRACK_CHLIST"
    export function SmartCar_track8(parameter: any, block: any) {
        let ch = parameter. TRACK_CH.code;
        Generator.addInclude('include_RL_PCF8574T', '#include "RL_PCF8574T.h"');
        Generator.addObject(`object_PCF8574_track`, `PCF8574 `,  `track(0x27);`);
        Generator.addSetup(`setup_track_begin`, `track.begin();`);
        Generator.addSetup(`setup_pcf8574_pinMode${ch}`, `track.pinMode(${ch}, INPUT);`);
        Generator.addCode(`track.digitalRead(${ch})`);
    }
    //% block="读取八路寻迹传感器缓存" blockType="command"
    export function SmartCar_track8val(parameter: any, block: any) {
        Generator.addInclude("include_RL_SmartCar", `#include"RL_SmartCar.h"`);
        Generator.addObject("object_SmartCar_track",`SmartCar`,  ` track;`);
        Generator.addSetup(`setup_track_track_init`, `track.track_init();`);
        Generator.addCode(`int sensorReadings[8];\n  track.readSensorValues(sensorReadings);`);
    }
    //% block="读取八路寻迹传感器位置（1-7）" blockType="reporter"
    export function SmartCar_track8middle(parameter: any, block: any) {
        Generator.addInclude("include_RL_SmartCar", `#include"RL_SmartCar.h"`);
        Generator.addObject("object_SmartCar_track",`SmartCar`,  ` track;`);
        Generator.addSetup(`setup_track_track_init`, `track.track_init();`);
        Generator.addCode(`track.calculateMiddleValue(sensorReadings)`);
    }
    //% block="LED灯[PIN]设置#[ONOFF]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_LED_PIN" 
    //% ONOFF.shadow="dropdown" ONOFF.options="ONOFFLIST" 
    export function SmartCar_digital_out(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let onoff = parameter.ONOFF.code;
        Generator.addSetup(`pinMode_${pin}`, `pinMode(${pin}, OUTPUT);`);
        Generator.addCode(`digitalWrite(${pin},${onoff});`);
    }
    //% block="设置全彩眼睛模块[PIN][COLOR0] [COLOR1]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_EYE_PIN" 
    //% COLOR0.shadow="colorPalette" 
    //% COLOR1.shadow="colorPalette"
    export function SmartCar_seteyeColor(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let color0=parameter.COLOR0.code; //获取传进来的值  
        let color1=parameter.COLOR1.code; //获取传进来的值
        var r0 = 0;
        var g0 = 0;
        var b0 = 0;
        try {
            if ( color0.length == 8 ) {//分别截取RGB值然后转换为数字值
                r0 = parseInt(color0.substring(2, 4), 16);
                g0 = parseInt(color0.substring(4, 6), 16);
                b0 = parseInt(color0.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }
        var r1 = 0;
        var g1 = 0;
        var b1 = 0;
        try {
            if ( color1.length == 8 ) {//分别截取RGB值然后转换为数字值
                r1 = parseInt(color1.substring(2, 4), 16);
                g1 = parseInt(color1.substring(4, 6), 16);
                b1 = parseInt(color1.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_DFRobot_NeoPixel_eye`, `DFRobot_NeoPixel `,  ` eye;`);
        Generator.addSetup(`setup_eye_begin`, `eye.begin(${pin},2);`);
        Generator.addCode(`eye.setPixelColor(0,${r0},${g0},${b0});`);
        Generator.addCode(`eye.setPixelColor(1,${r1},${g1},${b1});`);
    }


    //% block="氛围全彩灯 [PIN]设置亮度[BRIGHTNESS]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_FOURRGB_PIN" 
    //% BRIGHTNESS.shadow="range" BRIGHTNESS.params.min=0 BRIGHTNESS.params.max=255 BRIGHTNESS.defl=50
    export function SmartCar_color_ledbar_Brightness(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let brt = parameter.BRIGHTNESS.code;

        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_DFRobot_NeoPixel`,  `DFRobot_NeoPixel `,  ` fourrgb;`);
        Generator.addSetup(`setup_fourrgb_begin`, `fourrgb.begin(${pin},4);`);
        Generator.addCode(`fourrgb.setBrightness(${brt});`);
    }

    //% block="氛围全彩灯 [PIN]设置灯号[NUM] 颜色[COLOR]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_FOURRGB_PIN" 
    //% NUM.shadow="range" NUM.params.min=0 NUM.params.max=3 NUM.defl=0
    //% COLOR.shadow="colorPalette" 
    export function SmartCar_color_ledbar_setPixelColor(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let num = parameter.NUM.code;
        let color=parameter.COLOR.code; //获取传进来的值
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }

      Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
      Generator.addObject(`object_DFRobot_NeoPixel`,  `DFRobot_NeoPixel `,  ` fourrgb;`);
      Generator.addSetup(`setup_fourrgb_begin`, `fourrgb.begin(${pin},4);`);
      Generator.addCode(`fourrgb.setPixelColor(${num},${r},${g},${b});`);
    }

    //% block="氛围全彩灯 [PIN]设置灯号[NUM1]到[NUM2] 颜色[COLOR]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_FOURRGB_PIN" 
    //% NUM1.shadow="range" NUM1.params.min=0 NUM1.params.max=3 NUM1.defl=0
    //% NUM2.shadow="range" NUM2.params.min=0 NUM2.params.max=3 NUM2.defl=3
    //% COLOR.shadow="colorPalette" 
    export function SmartCar_color_ledbar_setRangeColor(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let num1 = parameter.NUM1.code;
        let num2 = parameter.NUM2.code;
        let color=parameter.COLOR.code; //获取传进来的值
      Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
      Generator.addObject(`object_DFRobot_NeoPixel`,  `DFRobot_NeoPixel `,  ` fourrgb;`);
      Generator.addSetup(`setup_fourrgb_begin`, `fourrgb.begin(${pin},4);`);
      Generator.addCode(`fourrgb.setRangeColor(${num1},${num2},${color});`);
    }
    //% block="氛围全彩灯 [PIN]设置灯号[NUM1]到[NUM2] 显示渐变颜色 色调从[COLOR1]到[COLOR2]" blockType="command" 
    //% PIN.shadow="dropdown" PIN.options="SmartCar_FOURRGB_PIN" 
    //% NUM1.shadow="range" NUM1.params.min=0 NUM1.params.max=3 NUM1.defl=0
    //% NUM2.shadow="range" NUM2.params.min=0 NUM2.params.max=3 NUM2.defl=3
    //% COLOR1.shadow="range" COLOR1.params.min=1 COLOR1.params.max=360 COLOR1.defl=1
    //% COLOR2.shadow="range" COLOR2.params.min=1 COLOR2.params.max=360 COLOR2.defl=360
    export function SmartCar_color_ledbar_showRainbow(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let num1 = parameter.NUM1.code;
        let num2 = parameter.NUM2.code;
        let color1=parameter.COLOR1.code;
        let color2=parameter.COLOR2.code;
      Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
      Generator.addObject(`object_DFRobot_NeoPixel`,  `DFRobot_NeoPixel `,  ` fourrgb;`);
      Generator.addSetup(`setup_fourrgb_begin`, `fourrgb.begin(${pin},4);`);
      Generator.addCode(`fourrgb.showRainbow(${num1},${num2},${color1},${color2});`);
    }
    //% block="氛围全彩灯 [PIN] 全部熄灭" blockType="command" 
    //% PIN.shadow="dropdown" PIN.options="SmartCar_FOURRGB_PIN" 
    export function SmartCar_color_ledbar_clear(parameter: any, block: any) {
        let pin = parameter.PIN.code;
      Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
      Generator.addObject(`object_DFRobot_NeoPixel`,  `DFRobot_NeoPixel `,  ` fourrgb`);
      Generator.addSetup(`setup_fourrgb_begin`, `fourrgb.begin(${pin},4);`);
      Generator.addCode(`fourrgb.clear();`);
    }
    //% block="无源蜂鸣器 [PIN]频率#[TONE] 节拍#[BEAT]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_TONE_PIN" 
    //% TONE.shadow="note" TONE.defl=247
    //% BEAT.shadow="dropdown" BEAT.options="BEATLIST" 
    export function SmartCar_speaker_tone(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let tone = parameter.TONE.code;
        let beat = parameter.BEAT.code;
        Generator.addInclude('include_NewTone', '#include <NewTone.h>');
        Generator.addSetup(`pinMode_${pin}`, `pinMode(${pin}, OUTPUT);`);
        Generator.addCode(`NewTone(${pin},${tone},${beat});`);
    }
    //% block="无源蜂鸣器 [PIN]停止播放 延时[MS]ms" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_TONE_PIN" 
    //% MS.shadow="number" MS.defl="0" 
    export function SmartCar_speaker_Notone(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let ms = parameter.MS.code;
        Generator.addInclude('include_NewTone', '#include <NewTone.h>');
        Generator.addSetup(`pinMode_${pin}`, `pinMode(${pin}, OUTPUT);`);
        Generator.addCode(`NewNoTone(${pin});\n  delay(${ms});`);
    }
    //% block="无源蜂鸣器 [PIN]播放#[MUSIC]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_TONE_PIN" 
    //% MUSIC.shadow="dropdown" MUSIC.options="MUSICLIST" 
    export function SmartCar_speaker_music(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let music = parameter.MUSIC.code;
        Generator.addInclude('include_NewTone', '#include <NewTone.h>');
        Generator.addInclude('include_pitches', '#include "pitches.h"\n#include "themes.h"\n');
        Generator.addSetup(`pinMode_${pin}`, `pinMode(${pin}, OUTPUT);`);
        Generator.addCode(`Play_${music}(${pin});`);
    }
    //% block="马达 通道#[ID] 速度±255[SPEED]" blockType="command"
    //% ID.shadow="dropdown" ID.options="MCHLIST" 
    //% SPEED.shadow="range" SPEED.params.min=-255 SPEED.params.max=255 SPEED.defl=0
    export function SmartCar_motor(parameter: any, block: any) {
        let id = parameter.ID.code;
        let speed = parameter.SPEED.code;

        Generator.addInclude('include_RL_SmartCar', '#include "RL_SmartCar.h"');
        Generator.addObject(`object_SmartCar_pwm`, `SmartCar`,  ` pwm;`);
       // Generator.addSetup(`setup_pwm_init`, `pwm.motor_init();`);
        Generator.addCode(`pwm.motor_speed(${id},${speed});`);
    }
    //% extenralFunc
    export function getBuiltinFunc_() {
        return [
            {matrix: "00000000000000000011100000011100001110000001110000111000000111000000000000000000000000000000000000000100001000000000001111000000"},
            {matrix: "01000100001000100010100000010100000100000000100000101000000101000100010000100010000000000000000000000000000000000000001111000000"},
            {matrix: "00000000000000000000000000000000000000000000110000110000000011000000000000000000000000000000000000010000000010000000111111110000"},
            {matrix: "00110000000011000100100000010010010010000001001001001000000100100011000000001100000000000000000000001000000000000000111111110000"},
            {matrix: "00000000000000000011100000010100001010000000100000111000000101000000000000000000000000000000000000010000000010000000111111110000"}
        ]
    }

    function binaryToHex(binaryStr) {
    // 去掉 "0B" 前缀
    binaryStr = binaryStr.replace(/^0B/, '');

    // 确保二进制字符串长度是 4 的倍数
    while (binaryStr.length % 4!== 0) {
        binaryStr = '0' + binaryStr;
    }

    const hexChars = '0123456789ABCDEF';
    let hex = '';
    // 每 4 位一组进行处理
    for (let i = 0; i < binaryStr.length; i += 4) {
        const binaryChunk = binaryStr.slice(i, i + 4);
        // 将 4 位二进制数转换为十进制数
        const decimal = parseInt(binaryChunk, 2);
        // 将十进制数转换为十六进制字符
        hex += hexChars[decimal];
    }
    return  hex;
    }


    //% block="表情面板点阵 [PIN]设置亮度[BRIGHTNESS]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_MATRIX_PIN"  
    //% BRIGHTNESS.shadow="range" BRIGHTNESS.params.min=0 BRIGHTNESS.params.max=255 BRIGHTNESS.defl=50
    export function SmartCar_setMatrix_BRIGHTNESS(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let brightness = parameter.BRIGHTNESS.code;
        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include  <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix `, `Adafruit_NeoMatrix`,  ` matrix;`);
        Generator.addSetup(`setup_matrix_begin`, `matrix.begin(${pin},128);`);
        Generator.addSetup(`setup_matrix_setBrightness`, `matrix.setBrightness(${brightness});`);

    }

    //% block="表情面板点阵 [PIN][MT]颜色[COLOR]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_MATRIX_PIN" 
    //% MT.shadow="matrix" MT.params.row=8 MT.params.column=16 MT.defl="00000000000000000011100000011100001110000001110000111000000111000000000000000000000000000000000000000100001000000000001111000000"
    //% MT.params.builtinFunc="getBuiltinFunc_" 
    //% COLOR.shadow="colorPalette"

    export function SmartCar_setMatrix_MT(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let matrix = parameter.MT.code;
        let matrix_num;
        let hz_matrix:any[] = [0,0,0,0,0,0,0,0];
        
        for (let i = 0; i < 16; i++) {

         hz_matrix[i] = binaryToHex(matrix.slice(0+i*8, 8+i*8));
         
        }
        let color=parameter.COLOR.code; //获取传进来的值  
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }
        
        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix `, `Adafruit_NeoMatrix`,  ` matrix;`);
        Generator.addSetup(`setup_matrix_begin`, `matrix.begin(${pin},128);`);

        Generator.addInclude(`matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}${hz_matrix[5]}${hz_matrix[6]}${hz_matrix[7]}${hz_matrix[8]}${hz_matrix[9]}${hz_matrix[10]}${hz_matrix[11]}${hz_matrix[12]}${hz_matrix[13]}${hz_matrix[14]}${hz_matrix[15]}`, `uint8_t matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}${hz_matrix[5]}${hz_matrix[6]}${hz_matrix[7]}${hz_matrix[8]}${hz_matrix[9]}${hz_matrix[10]}${hz_matrix[11]}${hz_matrix[12]}${hz_matrix[13]}${hz_matrix[14]}${hz_matrix[15]}[][16] = {{0x${hz_matrix[0]},0x${hz_matrix[1]},0x${hz_matrix[2]},0x${hz_matrix[3]},0x${hz_matrix[4]},0x${hz_matrix[5]},0x${hz_matrix[6]},0x${hz_matrix[7]},0x${hz_matrix[8]},0x${hz_matrix[9]},0x${hz_matrix[10]},0x${hz_matrix[11]},0x${hz_matrix[12]},0x${hz_matrix[13]},0x${hz_matrix[14]},0x${hz_matrix[15]}}};`);
        Generator.addCode(`matrix.fillScreen(matrix.Color(0,0,0));`);
        Generator.addCode(`matrix.drawBitmap(0, 0, matrix_${hz_matrix[0]}${hz_matrix[1]}${hz_matrix[2]}${hz_matrix[3]}${hz_matrix[4]}${hz_matrix[5]}${hz_matrix[6]}${hz_matrix[7]}${hz_matrix[8]}${hz_matrix[9]}${hz_matrix[10]}${hz_matrix[11]}${hz_matrix[12]}${hz_matrix[13]}${hz_matrix[14]}${hz_matrix[15]}[0], 16, 8, matrix.Color(${r}, ${g}, ${b}));`);
        Generator.addCode(`matrix.show();`);
    }
    //% block="表情面板点阵[PIN] [STR]颜色[COLOR]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_MATRIX_PIN" 
    //% STR.shadow="string" STR.defl="hello ruilong!"
    //% COLOR.shadow="colorPalette"
    //% BRIGHTNESS.shadow="range" BRIGHTNESS.params.min=0 BRIGHTNESS.params.max=255 BRIGHTNESS.defl=50
    export function SmartCar_setMatrix_STR(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let str = parameter.STR.code;
        let length = str.length;
        let color=parameter.COLOR.code; //获取传进来的值  
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }

        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix `, `Adafruit_NeoMatrix`,  ` matrix;`);
        Generator.addSetup(`setup_matrix_begin`, `matrix.begin(${pin},128);`);

        Generator.addSetup(`setup_matrix_setTextWrap`, `matrix.setTextWrap(false);`);
        Generator.addCode(`matrix.scrollDisplay(${str}, ${length}*5+2,matrix.Color(${r}, ${g}, ${b}));`);

    }
    //% block="表情面板点阵 [PIN]设置X[X]Y[Y]颜色[COLOR]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_MATRIX_PIN" 
    //% X.shadow="range" X.params.min=0 X.params.max=16 X.defl=0
    //% Y.shadow="range" Y.params.min=0 Y.params.max=8 Y.defl=0
    //% COLOR.shadow="colorPalette"
    export function SmartCar_setMatrix_PIXEL(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let x = parameter.X.code;
        let y = parameter.Y.code;
        let color=parameter.COLOR.code; //获取传进来的值  
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }
        
        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix `, `Adafruit_NeoMatrix`,  ` matrix`);
        Generator.addSetup(`setup_matrix_begin`, `matrix.begin(${pin},128);`);

        Generator.addCode(`matrix.drawPixel(15-${x},${y},matrix.Color(${r}, ${g}, ${b}));`);
        Generator.addCode(`matrix.show();\n`);

    }

    //% block="表情面板点阵 [PIN]填充全部 颜色[COLOR]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="SmartCar_MATRIX_PIN" 
    //% COLOR.shadow="colorPalette"
    export function SmartCar_setMatrix_FILLSCREEM(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let color=parameter.COLOR.code; //获取传进来的值  
        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }

        Generator.addInclude('include_Adafruit_GFX', '#include <Adafruit_GFX.h>'); 
        Generator.addInclude('include_RL_NeoMatrix', '#include <RL_NeoMatrix.h>');   
        Generator.addInclude('include_DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');
        Generator.addObject(`object_Adafruit_NeoMatrix_matrix `, `Adafruit_NeoMatrix`,  ` matrix;`);
        Generator.addSetup(`setup_matrix_begin`, `matrix.begin(${pin},128);`);

        Generator.addCode(`matrix.fillScreen(matrix.Color(${r}, ${g}, ${b}));`);  
        Generator.addCode(`matrix.show();\n`);

    }


    //% block="初始化RFID模块 接口I2C 地址0x28" blockType="command"
    export function SmartCar_RFID_Setup(parameter: any, block: any) {
  
        Generator.addInclude('include_Wire', '#include <Wire.h>');
        Generator.addInclude('include_RL_MFRC522_I2C', '#include <RL_MFRC522_I2C.h>');
        Generator.addObject("object_MFRC522_rfid", `MFRC522 `, ` rfid(0x28,-1);`);
        Generator.addSetup(`Wire_begin`, `Wire.begin();`);
        Generator.addSetup(`rfid_PCD_Init`, `rfid.PCD_Init();`);
    }

    //% block="RFID检测到卡片？" blockType="boolean"
    export function SmartCar_RFID_available() {
        Generator.addCode(`rfid.IsNewCard()`)
    }
    //% block="读取RFID模块UID" blockType="reporter"
    export function SmartCar_RFIDdata(parameter: any, block: any) {
    Generator.addCode(`rfid.ReadCardUID()`);
    }
    
    //% block="读取RFID模块 数据块[dataBlock]第[ByteID]字节" blockType="reporter"
    //% dataBlock.shadow="range"   dataBlock.params.min=0    dataBlock.params.max=62    dataBlock.defl=0  
    //% ByteID.shadow="range"  ByteID.params.min=0    ByteID.params.max=16    ByteID.defl=0  
    export function SmartCar_RFIDReadCardbyte(parameter: any, block: any) {
        let dataBlock = parameter.dataBlock.code;
        let ByteID = parameter.ByteID.code;
    Generator.addCode(`rfid.ReadCardbyte( ${dataBlock}, ${ByteID})`);
    }


    //% block="RFID模块 数据块[dataBlock]第[ByteID]字节写入[data]" blockType="command"
    //% dataBlock.shadow="range"   dataBlock.params.min=0    dataBlock.params.max=62    dataBlock.defl=0  
    //% ByteID.shadow="range"  ByteID.params.min=0   ByteID.params.max=16    ByteID.defl=0  
    //% data.shadow="range"   data.params.min=0    data.params.max=255    data.defl=100  
    export function SmartCar_RFIDWriteCardbyte(parameter: any, block: any) {
        let dataBlock = parameter.dataBlock.code;
        let ByteID = parameter.ByteID.code;
        let data = parameter.data.code;
        Generator.addCode(`rfid.WriteCardbyte( ${dataBlock}, ${ByteID},${data});`);
    }

    //% block="RFID模块 终止卡片通讯" blockType="command"
      export function SmartCar_RFIDStopComm(parameter: any, block: any) {
        Generator.addCode(`rfid.PICC_HaltA();\nrfid.PCD_StopCrypto1();`);
    }

    //% block="ESP32 FPV图传摄像头模块 初始化" blockType="command"
    export function FPV_Init(parameter: any, block: any) {
        Generator.addInclude('FPV', '#include "RL_FPV_MP.h"');
        Generator.addObject(`fpvSerial`, `FPV `, `fpvSerial;//UART1`);
        Generator.addSetup(`fpvSerial.begin`, `fpvSerial.begin(&Serial1,2,-1);//定义默认接收管脚 2号`);
        //Generator.addCode(`fpvSerial.FpvReadAll();`);
    }
    //% block="ESP32 FPV图传摄像头模块 读取FPV控制数据" blockType="reporter"
    export function FPV_ReadAll(parameter: any, block: any) {
       
        Generator.addCode(`fpvSerial.FpvReadAll()`);
    }

    //% block="ESP32 FPV图传摄像头模块 返回FPV[CH]的数据" blockType="reporter"
    //% CH.shadow="dropdown" CH.options="FPV_DATACH"
    export function FPV_ReadByte(parameter: any, block: any) {
        let ch = parameter.CH.code;

        Generator.addCode(`fpvSerial.FpvReadByte(${ch})`);
    }

    //% block="PID算法 初始化 输入[Input]输出[Output]期望值[Setpoint] Kp[Kp]Ki[Ki]Kd[Kd]" blockType="command"
    //% Input.shadow="normal"   Input.defl="Input"
    //% Output.shadow="normal"   Output.defl="Output"
    //% Setpoint.shadow="normal"   Setpoint.defl="Setpoint"
    //% Kp.shadow="normal"   Kp.defl="0"
    //% Ki.shadow="normal"   Ki.defl="0"
    //% Kd.shadow="normal"   Kd.defl="0"
    export function PID_Init(parameter: any, block: any) {
        let input = parameter.Input.code;
        let output = parameter.Output.code;
        let setpoint = parameter.Setpoint.code;
        let kp = parameter.Kp.code;
        let ki = parameter.Ki.code;
        let kd = parameter.Kd.code;
        Generator.addInclude('include_RL_PID', '#include <RL_PID.h>');
        Generator.addObject(`Object_PID`, `PID `, `myPID(const_cast<float*>(&${input}), const_cast<float*>(&${output}), const_cast<float*>(&${setpoint}), ${kp}, ${ki}, ${kd}, DIRECT);`);
        Generator.addSetup(`myPID_SetMode`, `myPID.SetMode(AUTOMATIC);`);
    }
    //% block="PID算法 计算进行中" blockType="command"
    export function PID_Compute(parameter: any, block: any) {
        Generator.addInclude('include_RL_PID', '#include <RL_PID.h>');
        Generator.addCode(`myPID.Compute();`);
    }

   //% block="PID算法 设置采样时间[Time]ms" blockType="command"
   //% Time.shadow="number"   Time.defl="100"
    export function PID_SetSampleTime(parameter: any, block: any) {
        let time = parameter.Time.code;
        Generator.addInclude('include_RL_PID', '#include <RL_PID.h>');
        Generator.addCode(`myPID.SetSampleTime(${time});`);
    }

   //% block="PID算法 设置输出限制最小[MIN]最大[MAX]" blockType="command"
   //% MIN.shadow="number"   MIN.defl="0"
   //% MAX.shadow="number"   MAX.defl="255"
    export function PID_SetOutputLimits(parameter: any, block: any) {
        let min = parameter.MIN.code;
        let max = parameter.MAX.code;
        Generator.addInclude('include_RL_PID', '#include <RL_PID.h>');
        Generator.addCode(`myPID.SetOutputLimits(${min}, ${max});`);
    }

    //% block="PID算法 重新设置参数 Kp[Kp]Ki[Ki]Kd[Kd]" blockType="command"
    //% Kp.shadow="normal"   Kp.defl="0"
    //% Ki.shadow="normal"   Ki.defl="0"
    //% Kd.shadow="normal"   Kd.defl="0"
    export function PID_SetTunings(parameter: any, block: any) {
        let kp = parameter.Kp.code;
        let ki = parameter.Ki.code;
        let kd = parameter.Kd.code;
        Generator.addInclude('include_RL_PID', '#include <RL_PID.h>');
        Generator.addCode(`myPID.SetTunings(${kp}, ${ki},${kd});`);
    }
}
